package com.sky.service.impl;


import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ReprotMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private ReprotMapper reportMapper;
    @Autowired
    private ReprotServiceImpl reprotService;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 今日数据
     * @return
     * @param begin
     * @param end
     */
    public BusinessDataVO businessDate(LocalDateTime begin, LocalDateTime end) {
        //查询新增用户
        HashMap<Object, Object> map = new HashMap<>();
        Object endTime = map.put("end",end);
        Object beginTime = map.put("begin",begin);
        Integer newUsers = reportMapper.queryUsers(map);

        //查询订单完成率
        OrderReportVO orderReportVO = reprotService.orderStatistics(begin.toLocalDate(),end.toLocalDate());
        Double orderCompletionRate = orderReportVO.getOrderCompletionRate();
        //查询营业额
        TurnoverReportVO turnoverReportVO = reprotService.turnoverStatistics(begin.toLocalDate(),end.toLocalDate());
        String turnoverList = turnoverReportVO.getTurnoverList();
        String[] split = turnoverList.split(",");
        Double turnover =0.0;
        Double temp = 0.0;
        for (String s : split) {
            if (s.equals("")){
                turnover+=temp;
                continue;
            }
            turnover+=Double.parseDouble(s);
        }
        /*        double turnover = Double.parseDouble(turnoverList);*/

        //查询有效订单数
        Integer validOrderCount = orderReportVO.getValidOrderCount();

        //查询平均客单价
        double unitPrice = 0.0;
        if (validOrderCount!=null&&validOrderCount!=0){
             unitPrice = turnover/validOrderCount;
        }

        return BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 工作台订单管理
     * @return
     */
    public OrderOverViewVO overviewVoOrders() {
        LocalDate now = LocalDate.now();
        LocalDateTime max = LocalDateTime.of(now, LocalTime.MAX);
        LocalDateTime min = LocalDateTime.of(now, LocalTime.MIN);
        //全部订单
        HashMap<Object, Object> map = new HashMap<>();
        Object endTime = map.put("end",max);
        Object beginTime = map.put("begin",min);
        map.put("status",null);
        Integer allOrders = reportMapper.queryOrders(map);

        //已取消订单数
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = reportMapper.queryOrders(map);
        //已完成订单数
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = reportMapper.queryOrders(map);
        //待派送订单数
        map.put("status", Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = reportMapper.queryOrders(map);
        //待接单订单数
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = reportMapper.queryOrders(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 菜品管理
     * @return
     */
    public DishOverViewVO overviewDish() {

       Integer discontinued= dishMapper.queryByStatus(StatusConstant.DISABLE);
       Integer sold= dishMapper.queryByStatus(StatusConstant.ENABLE);
       return DishOverViewVO.builder()
               .discontinued(discontinued)
               .sold(sold)
               .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued= setmealMapper.queryByStatus(StatusConstant.DISABLE);
        Integer sold= setmealMapper.queryByStatus(StatusConstant.ENABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
